package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/models"
	"goravel/app/services/menu"
)

type MenuController struct {
	//Dependent services
	menu.MenuService
}

func NewMenuController() *MenuController {
	return &MenuController{
		//Inject services
	}
}

// Index returns the menus list.
// @Summary Get menus
// @Description 获取菜单.
// @Tags MenuController
// @Security BearerAuth
// @Accept json
// @Produce json
// @Id MenuIndex
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} common.SuccessResponse "Successfully retrieved Menus ."
// @Failure 500 {object} common.ErrorResponse "Internal server error."
// @OperationID Menu
// @Router /api/admin/menu [get]
func (r *MenuController) Index(ctx http.Context) http.Response {
	menus := []models.Menu{}
	facades.Orm().Query().Model(&models.Menu{}).Find(&menus)
	return httpfacade.NewResult(ctx).Success("", menus)
}

// Init returns the menus list.
// @Summary Get menus
// @Description 获取菜单.
// @Tags MenuController
// @Security BearerAuth
// @Accept json
// @Produce json
// @Id MenuInit
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} common.SuccessResponse "Successfully retrieved Menus ."
// @Failure 500 {object} common.ErrorResponse "Internal server error."
// @OperationID Menu
// @Router /api/admin/menu/init [get]
func (r *MenuController) Init(ctx http.Context) http.Response {

	err := r.ParseFile()
	if err != nil {
		httpfacade.NewResult(ctx).Error(500, "", err.Error())
	}
	return httpfacade.NewResult(ctx).Success("初始化成功", nil)
}

// GetTree returns the menus.
// @Summary Get menus
// @Description 获取菜单.
// @Tags MenuController
// @Security BearerAuth
// @Accept json
// @Produce json
// @Id MenuGetTree
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} common.SuccessResponse "Successfully retrieved Menus ."
// @Failure 500 {object} common.ErrorResponse "Internal server error."
// @OperationID Profile
// @Router /api/admin/menu/tree [get]
func (r *MenuController) GetTree(ctx http.Context) http.Response {
	menus, err := r.RecursionGet()
	if err != nil {
		httpfacade.NewResult(ctx).Error(500, "", err.Error())
	}
	return httpfacade.NewResult(ctx).Success("", menus)
}

// InitPermissions init permissions.
// @Summary Get menus
// @Description 初始化权限.
// @Tags MenuController
// @Security BearerAuth
// @Accept json
// @Produce json
// @Id MenuInitPermissions
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} common.SuccessResponse "Successfully retrieved Menus ."
// @Failure 500 {object} common.ErrorResponse "Internal server error."
// @OperationID Profile
// @Router /api/admin/permission/init [get]
func (r *MenuController) InitPermissions(ctx http.Context) http.Response {
	err := r.BuildPermissions()
	if err != nil {
		httpfacade.NewResult(ctx).Error(500, "", err.Error())
	}
	return httpfacade.NewResult(ctx).Success("权限初始化成功", nil)
}

// GetPermissions get all permissions.
// @Summary Get menus
// @Description 获取权限表.
// @Tags MenuController
// @Security BearerAuth
// @Accept json
// @Produce json
// @Id MenuGetPermissions
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} common.SuccessResponse "Successfully retrieved Menus ."
// @Failure 500 {object} common.ErrorResponse "Internal server error."
// @OperationID Profile
// @Router /api/admin/permission [get]
func (r *MenuController) GetPermissions(ctx http.Context) http.Response {
	err := r.BuildPermissions()
	if err != nil {
		httpfacade.NewResult(ctx).Error(500, "", err.Error())
	}
	return httpfacade.NewResult(ctx).Success("权限初始化成功", nil)
}
